package net.yuyoo.testing;

import java.io.IOException;

import net.spy.memcached.AddrUtil;
import net.spy.memcached.MemcachedClient;
 
  
public class TestMemCache {    
  
    static int threads; // 运行的测试线程数   
  
    static int runs; // 每个线程运行的次数   
  
    static int size; // 设置到memcache中的数据包大小，单位k   
  
    static Integer myLock;// 锁定以下计数器   
  
    static long putTimes = 0; // put总时间，单位微秒   
  
    static long getTimes = 0; // get总时间，单位微秒   
  
    /**  
     * @param args  
     */  
    public static void main(String[] args) {   
    	
        String[] serverlist = { "192.168.0.56:11211", "192.168.0.56:11212" };   
   
        threads = 1;//Integer.parseInt(args[0]);   
        runs = 15000;// Integer.parseInt(args[1]);   
        size = 1;//100 * Integer.parseInt(args[2]);   
  
        myLock = new Integer(threads);   
  
        for (int i = 0; i < threads; i++) {   
            Thread thread = new WorkerThread();   
            thread.start();   
        }   
    }   
  
    private static class WorkerThread extends Thread {   
  
        // 构造函数   
        WorkerThread() {   
        }   
  
        public void run() {   
            // get client instance   
        	MemcachedClient mc = null;
			try {
				mc = new MemcachedClient(AddrUtil.getAddresses("10.20.56.64:11211"));
			} catch (IOException e) {
				e.printStackTrace();
			}   
     
            // get object to store   
            int[] obj = new int[size];   
            for (int i = 0; i < size; i++) {   
                obj[i] = i;   
            }   
  
            String[] keys = new String[runs];   
            for (int i = 0; i < runs; i++) {   
                keys[i] = "test_key" + i;   
            }   
  
            for (int i = 0; i < runs; i++) {   
               // mc.delete(keys[i]);
            	mc.delete(keys[i]);
            }   
  
            long startTime = System.currentTimeMillis();   
            for (int i = 0; i < runs; i++) {   
                //mc.set(keys[i], obj);
            	mc.set(keys[i], 3600, obj);
            }   
            long time = System.currentTimeMillis() - startTime;   
  
            synchronized (myLock) {   
                putTimes += time;   
            }   
  
            startTime = System.currentTimeMillis();   
            for (int i = 0; i < runs; i++) {   
                mc.get(keys[i]);   
            }   
            time = System.currentTimeMillis() - startTime;   
  
            synchronized (myLock) {   
                getTimes += time;   
                myLock--;   
  
                if (myLock.equals(0)) {   
                    System.out.println("测试完成! 启动线程数:" + threads   
                            + ", 每线程执行测试数量: " + runs + ", 测试数据大小(byte):" + size);   
  
                    System.out.println("put处理时间:" + putTimes   
                            + "微秒，处理put速度: 每秒 " + runs * threads * 1000 / putTimes   
                            + " 次");   
                    System.out.println("get处理时间:" + getTimes   
                            + "微秒，处理get速度: 每秒 " + runs * threads * 1000 / getTimes   
                            + " 次");    
                }   
            }   
        }   
    }   
  
}  